//引入WxValidate验证类
import WxValidate from '../../utils/WxValidate';
const app = getApp();
let ServiceUrl = app.globalData.ServiceUrl;
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
  data: {
    columns: ['子女', '兄弟', '朋友', '亲戚'],
    nexus:'子女',//关系
    isShow: false,
    form:{
      oldman_name:'',
      IDcard:'',
      oldman_phone:'',
      emergency_phone:''
    }
  },
  onCancel:function(){
    this.setData({isShow: false})
  },
  onConfirm:function(e){
    console.log(e.detail.value)
    this.setData({nexus: e.detail.value, isShow: false})
  },
  //关系
  nexusChange: function(){
    this.setData({isShow: true})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({ user_id: options.user_id});
    this.initValidate()
  },
//验证函数
initValidate(){
  const rules = {
    oldman_name:{
      required: true,
      rangelength: [2,4]
    },
    IDcard:{
      required: true,
      idcard: true
    },
    oldman_phone:{
      required: true,
      tel:true
    },
    emergency_phone:{
      required: true,
      tel:true
    },
  }
  const messages={
    oldman_name:{
      required: '请输入姓名',
      rangelength:'请输入2~4个文字的姓名'
    },
    IDcard:{
      required: '请输入身份证号码',
      idcard: '请输入正确的身份证号码'
    },
    oldman_phone:{
      required: '请输入手机号',
      tel: '请输入正确的手机号'
    },
    emergency_phone:{
      required: "请输入应急号码",
      tel: '请输入正确的手机号'
    },
  }
  this.WxValidate = new WxValidate(rules,messages)
},
  //报错
  showModal(error){
    wx.showModal({
      content:error.msg,
      showCancel: false,
      confirmColor: 'skyblue',
    })
  },
//调用验证函数
formSubmit: function(e){
  let that = this;
  console.log('form发生了submit事件，携带的数据为：',e.detail.value)
  const params = e.detail.value;
  //校验表单
  if(!that.WxValidate.checkForm(params)){
    const error = that.WxValidate.errorList[0]
    that.showModal(error)
    return false
  }
  console.log(e.detail.value)
  //console.log(that.options.uid)//用户user_id
  //检测是否登录
  if(that.data.user_id == null || that.data.user_id == ''){
    wx.showModal({
      title: "提示",
      content: "请先登录",
      showCancel:false,
      success:function(re){
        if(re.confirm){
          wx.navigateTo({
            url: './../index/index',
          })
        }
      }
    })
  }else{
    wx.showLoading({
      title: '提交中',
    })
    console.log(that.data.user_id)
    //提交数据
    wx.request({
      url: ServiceUrl + 'peopleAdd',
      data:{
          oldman_name: e.detail.value.oldman_name,
          IDcard: e.detail.value.IDcard,
          oldman_phone: e.detail.value.oldman_phone,
          emergency_phone: e.detail.value.emergency_phone,
          nexus: that.data.nexus,
          user_id: that.data.user_id
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success:function(res){
        wx.hideLoading();
        console.log(res)
        wx.hideLoading();
        if(res.data.state == 200){
          wx.showModal({
            title: "提示",
            content: "提交成功",
            showCancel: false,
            confirmColor: 'skyblue',
            success:function(re){
              if(re.confirm){
                wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })
        }else if(res.data.state == 400){
          wx.showModal({
            cancelColor: 'cancelColor',
            content: '提交失败，请重新提交',
            showCancel: false,
            confirmColor: 'skyblue',
            success (res){
            }
          });
        }
      },
      fail:function(res){
        wx.showModal({
          title:"提示",
          content:"发送请求失败",
          showCancel: false,
          confirmColor: 'skyblue',
        })
      }
    })
  }
  
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //关闭当前页面的时候，将值传递到上一个页面
    let that = this;
    //获取页面栈
    let pages = getCurrentPages();
    //获取所需页面
    let prevPage = pages[pages.length -2];//上一页
    //执行上个界面的onShow
    prevPage.onShow();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})